package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.shield;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerDynamicComponent;
import com.kaiwukj.android.ufamily.di.module.DynamicModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicReportTypeResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ShieldResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.ShieldListUrl)
public class ShieldListActivity extends BaseMvpActivity<ShieldPresenter> implements DynamicContract.ReportView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private ShieldListAdapter mListAdapter;

    private int page = 1;
    private int pageSize = 15;

    private boolean isLoadingMore = false;

    private int shieldPos = -1;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerDynamicComponent.builder()
                .appComponent(appComponent)
                .dynamicModule(new DynamicModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list_normal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("屏蔽列表");

        mListAdapter = new ShieldListAdapter();
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(1));
        mListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            shieldPos = position;
            mPresenter.shieldCancel(mListAdapter.getData().get(position).getResourceId());
        });
        rvList.setAdapter(mListAdapter);

        mListAdapter.setEmptyView(R.layout.widget_empty_common);

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadingMore = true;
                ++page;
                loadData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData();
            }
        });


    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loadData();
    }

    private void loadData() {
        mPresenter.fetchShieldList(page, pageSize);
    }

    @Override
    public void onFetchReportView(List<DynamicReportTypeResult> results) {

    }


    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
        hideLoading();
    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == ShieldPresenter.SHIELD) {
            showMessage("操作成功");
            if (shieldPos >= 0) {
                mListAdapter.removeAt(shieldPos);
                shieldPos = -1;
            }
        }
    }

    @Override
    public void onFetchShieldList(List<ShieldResult> results) {
        hideLoading();
        if (isLoadingMore) {
            isLoadingMore = false;
            mListAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            mListAdapter.setList(results);
            refreshLayout.finishRefresh(true);
        }
    }
}
